package com.app.lxx.pregnancyberth.service;

import android.app.IntentService;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.app.lxx.pregnancyberth.MyApplication;
import com.app.lxx.pregnancyberth.base.UrlManage;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.io.File;

public class UpdateService extends IntentService {

    public UpdateService() {
        super("UpdateService");
    }

    private String apkName = "app.apk";

    @Override
    protected void onHandleIntent(Intent intent) {
        while (true) {
            OkGo.<String>post(UrlManage.APP_URL+"api/index/checkupd")
                    .tag(this)
                    .params("type", "1")
                    .execute(new StringCallback() {

                        @Override
                        public void onSuccess(Response<String> response) {
                            Log.i("sssssss更新：", response.body());
                            try{
                                JSONObject jsonObject = JSON.parseObject(response.body());
                                if ("1".equals(jsonObject.getString("upd"))){
                                    Toast.makeText(MyApplication.context, "检测到有更新，开始更新下载...", Toast.LENGTH_LONG);
                                    //更新
                                    OkGo.<File>get(UrlManage.APP_URL+"")
                                            .execute(new FileCallback(getApkPath(), apkName) {
                                                @Override
                                                public void onSuccess(Response<File> response) {
                                                    Toast.makeText(MyApplication.context, "下载完毕，开始安装...", Toast.LENGTH_LONG);
                                                    Log.i("sssssss", "准备开始安装！！");
                                                    Intent intent = new Intent(Intent.ACTION_VIEW);
                                                    File file = new File(getApkPath()+apkName);
                                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                    Uri uri = Uri.fromFile(file);
                                                    intent.setDataAndType(uri, "application/vnd.android.package-archive");
                                                    Log.i("sssssss", "开始安装！！");
                                                    startActivity(intent);
                                                }
                                            });
                                }
                            }catch (Exception e){
                                Log.i("sssssss自动更新数据异常", e.getMessage()+"");
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                        }
                    });
            Log.i("sssssss自动更新", "5小时后自动更新");
            try {
//                Thread.sleep(1000*60*60*5);
                Thread.sleep(1000*60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 获取内置SD卡路径
     * @return
     */
    public String getApkPath() {
        return this.getExternalFilesDir("apk").getAbsolutePath();
    }
}
